package com.swz.dcrm.ui.approval;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.swz.dcrm.adpter.FragmentViewPagerAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Tab;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.car.CarResItemFragment;
import com.swz.dcrm.ui.main.TabEntity;
import com.swz.dcrm.ui.viewmodel.ApprovalViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.RxTimer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class ApprovalFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private List<String> titles;
    private List<Fragment> fragments;


    @Inject
    ApprovalViewModel approvalViewModel;

    @Inject
    MainViewModel mainViewModel;

    public static ApprovalFragment newInstance() {

        return new ApprovalFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())) {
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        approvalViewModel.getApprovalTabsResult(false).setValue(null);
        approvalViewModel.getApprovalTabs(false)
                .observe(this, observer);

    }
    Observer observer=new Observer<BaseResponse<List<Tab>>>() {
        @Override
        public void onChanged(BaseResponse<List<Tab>> listBaseResponse) {
            if(listBaseResponse==null){
                return;
            }


            approvalViewModel.getApprovalTabsResult(false).removeObserver(this);

            if (listBaseResponse.isSuccess()) {
                List<Tab> carTabs = listBaseResponse.getData();
                if(fragments!=null&&fragments.size()>0){
                    if(mainViewModel.isManager()){
                        for (int i = 0; i < carTabs.size(); i++) {
                            if (carTabs.get(i).getTypeCount() == 0) {
                                slidingTabLayout.showMsg(i, carTabs.get(i).getTypeCount());
                                slidingTabLayout.hideMsg(i);
                            }else {
                                slidingTabLayout.getMsgView(i).setBackgroundColor(Color.parseColor("#ff8730"));
                                slidingTabLayout.showMsg(i, carTabs.get(i).getTypeCount());
                            }

                        }
                    }
                    return;
                }
                fragments = new ArrayList<>();
                titles = new ArrayList<>();

                for (Tab carTab : carTabs) {
                    if(mainViewModel.isManager()){
                        slidingTabLayout.setTabSpaceEqual(false);
                        slidingTabLayout.setTabPadding(10);
                        titles.add(carTab.getName());//+"("+carTab.getTypeCount()+")"
                        fragments.add(ApprovalItemFragment.newInstance(carTab.getCode(),false));
                    }else {
                        titles.add(carTab.getName()+"("+carTab.getTypeCount()+")");//
                        if(carTab.getCode()==0){
                            fragments.add(ApprovalItemFragment.newInstance(carTab.getCode(),false));
                        }else {
                            fragments.add(ApprovalItemFragment.newInstance(carTab.getCode(),true));
                        }

                    }
                }
                viewPager.setAdapter(new FragmentViewPagerAdapter(getChildFragmentManager(), fragments, titles));


                slidingTabLayout.setViewPager(viewPager);

                if(mainViewModel.isManager()){
                    for (int i = 0; i < carTabs.size(); i++) {
                        if (carTabs.get(i).getTypeCount() == 0) {
                            slidingTabLayout.hideMsg(i);
                        }else {
                            slidingTabLayout.getMsgView(i).setBackgroundColor(Color.parseColor("#ff8730"));

                            slidingTabLayout.showMsg(i, carTabs.get(i).getTypeCount());
                            slidingTabLayout.setMsgMargin(i,3,6);
                        }

                    }
                }


            }

        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_approval;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.approval_title));
        if(mainViewModel.isManager()){
            tvRight.setText(getString(R.string.approval_record));
        }

        viewPager.setOffscreenPageLimit(4);

        return true;
    }

    @OnClick(R.id.tv_right)
    public void record(){
        go(null,R.id.action_approvalFragment_to_approvalRecordFragment,null);
    }
    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
